﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;

namespace Pixysoft.Framework.Reflection.Core
{
    class DynamicMethodCallMessage : DynamicMethodMessage, IDynamicMethodCallMessage
    {
        object[] paras = new object[] { };

        public DynamicMethodCallMessage(MethodInfo info, object[] paras)
            : base(info)
        {
            this.paras = paras;

            if (this.paras == null)
                this.paras = new object[] { };
        }

        public int InArgCount
        {
            get { return MethodInfo.GetParameters().Length; }
        }

        public object[] InArgs
        {
            get
            {
                return paras;
            }
        }

        public object GetInArg(int index)
        {
            if (index < 0)
                throw new Exception("index cannot be negative.");

            if (index > InArgCount - 1)
                throw new Exception(string.Format("index exist input range. range = {0}", InArgCount));

            if (index > paras.Length - 1)
            {
                ParameterInfo paraInfo = MethodInfo.GetParameters()[index];

                return Pixysoft.Tools.ParserHelper.GetNullValue(paraInfo.ParameterType);
            }

            return paras[index];
        }

        public string GetInArgName(int index)
        {
            if (index < 0)
                throw new Exception("index cannot be negative.");

            if (index > InArgCount - 1)
                throw new Exception(string.Format("index exist input range. range = {0}", InArgCount));

            ParameterInfo paraInfo = MethodInfo.GetParameters()[index];

            return paraInfo.Name;
        }

        public override void Dispose()
        {
            this.paras = null;

            base.Dispose();
        }
    }
}
